package com.lanbu.chat.service.impl;

import java.util.List;

import com.lanbu.common.annotation.DataScope;
import com.lanbu.common.core.domain.entity.SysUser;
import com.lanbu.common.exception.ServiceException;
import com.lanbu.common.utils.SecurityUtils;
import com.lanbu.common.utils.StringUtils;
import com.lanbu.common.utils.spring.SpringUtils;
import com.lanbu.system.domain.mongo.MongoApps;
import com.lanbu.system.domain.mongo.MongoDatasets;
import com.lanbu.system.service.impl.MongoAppsService;
import com.lanbu.system.service.impl.MongoDatasetsService;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.lanbu.chat.mapper.ImProblemLogMapper;
import com.lanbu.chat.domain.ImProblemLog;
import com.lanbu.chat.service.IImProblemLogService;

/**
 * 问题反馈Service业务层处理
 *
 * @author lanbu
 * @date 2024-09-15
 */
@Service
@RequiredArgsConstructor
public class ImProblemLogServiceImpl implements IImProblemLogService
{
    private final ImProblemLogMapper imProblemLogMapper;
    private final MongoAppsService mongoAppsService;
    @Autowired
    private MongoDatasetsService mongoDatasetsService;
    /**
     * 校验问题反馈是否有数据权限
     *
     * @param problemLogId 反馈问题id
     */
    @Override
    public void checkProblemLogDataScope(Long problemLogId)
    {
        if (!SysUser.isAdmin(SecurityUtils.getUserId()))
        {
            ImProblemLog imProblemLog = new ImProblemLog();
            imProblemLog.setId(problemLogId);
            List<ImProblemLog> imProblemLogs = SpringUtils.getAopProxy(this).selectImProblemLogList(imProblemLog);
            if (StringUtils.isEmpty(imProblemLogs))
            {
                throw new ServiceException("没有权限访问问题反馈数据！");
            }
        }
    }

    @Override
    public ImProblemLog selectImProblemLogById(Long id)
    {
        return imProblemLogMapper.selectImProblemLogById(id);
    }

    @Override
    @DataScope(deptAlias = "su", userAlias = "su")
    public List<ImProblemLog> selectImProblemLogList(ImProblemLog imProblemLog)
    {
        List<ImProblemLog> imProblemLogs = imProblemLogMapper.selectImProblemLogList(imProblemLog);
        imProblemLogs.stream().forEach(t -> {
            String fastAppId = t.getFastAppId();
          //  MongoApps mongoApps = mongoAppsService.findById(fastAppId).orElse(new MongoApps());
            List<MongoDatasets> datasets = mongoDatasetsService.findByAppId(fastAppId);
            t.setDatasets(datasets);
        // t.setFastAppName(mongoApps.getName());
        });
        return imProblemLogs;
    }

    @Override
    public int insertImProblemLog(ImProblemLog imProblemLog)
    {
        return imProblemLogMapper.insertImProblemLog(imProblemLog);
    }

    @Override
    public int updateImProblemLog(ImProblemLog imProblemLog)
    {
        return imProblemLogMapper.updateImProblemLog(imProblemLog);
    }

    @Override
    public int deleteImProblemLogByIds(Long[] ids)
    {
        for (Long id : ids) {
            checkProblemLogDataScope(id);
        }
        return imProblemLogMapper.deleteImProblemLogByIds(ids);
    }

    @Override
    public int deleteImProblemLogById(Long id)
    {
        return imProblemLogMapper.deleteImProblemLogById(id);
    }
}
